package com.example.travel.controller;

import com.example.travel.entity.TravelPackage;
import com.example.travel.repository.TravelPackageRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/travel-packages")
@Tag(name = "旅游套餐管理", description = "旅游套餐的增删改查接口")
public class TravelPackageController {

    @Autowired
    private TravelPackageRepository travelPackageRepository;

    @GetMapping
    @Operation(summary = "获取所有旅游套餐", description = "返回所有可用的旅游套餐列表")
    public List<TravelPackage> getAllTravelPackages() {
        return travelPackageRepository.findAll();
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取单个旅游套餐", description = "根据ID获取特定的旅游套餐详情")
    public ResponseEntity<TravelPackage> getTravelPackageById(
            @Parameter(description = "旅游套餐ID", required = true) @PathVariable Long id) {
        return travelPackageRepository.findById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PostMapping
    @Operation(summary = "创建旅游套餐", description = "创建新的旅游套餐")
    public TravelPackage createTravelPackage(
            @Parameter(description = "旅游套餐信息", required = true) @RequestBody TravelPackage travelPackage) {
        return travelPackageRepository.save(travelPackage);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新旅游套餐", description = "根据ID更新现有的旅游套餐信息")
    public ResponseEntity<TravelPackage> updateTravelPackage(
            @Parameter(description = "旅游套餐ID", required = true) @PathVariable Long id,
            @Parameter(description = "更新后的旅游套餐信息", required = true) @RequestBody TravelPackage travelPackage) {
        return travelPackageRepository.findById(id)
                .map(existingPackage -> {
                    travelPackage.setId(id);
                    return ResponseEntity.ok(travelPackageRepository.save(travelPackage));
                })
                .orElse(ResponseEntity.notFound().build());
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除旅游套餐", description = "根据ID删除旅游套餐")
    public ResponseEntity<Void> deleteTravelPackage(
            @Parameter(description = "旅游套餐ID", required = true) @PathVariable Long id) {
        return travelPackageRepository.findById(id)
                .map(travelPackage -> {
                    travelPackageRepository.delete(travelPackage);
                    return ResponseEntity.ok().<Void>build();
                })
                .orElse(ResponseEntity.notFound().build());
    }
} 